﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sustainalytics.Entities;
using Sustainalytics.Synchronization.Validation.Entities.CorporateGovernance;
using Sustainalytics.Synchronization.Validation.ValidationRules.Common;

namespace Sustainalytics.Synchronization.Validation.ValidationRules.CorporateGovernance
{
    public class GovernanceMissingScoresRule : Rule<GovernanceValidationProfile>, IHashSetDependent<string>
    {
        private HashSet<string> indicatorsHashSet;

        public override Severity Severity => Severity.Red;
        public override BusinessResearchProduct BusinessResearchProduct => BusinessResearchProduct.CorporateGovernance;
        public override string Code => "RR6";
        public override string InformationalMessage => "Missing scores for all indicators";

        public GovernanceMissingScoresRule()
        {
            For(x => x.GovernanceIndicators).When(Validator).WithKey(x => x.CompanyId)
                .WithMessage("The company {0} #{1}# does not have score for all indicators");
        }

        public void SetDependency(HashSet<string> hashSet)
        {
            indicatorsHashSet = hashSet;
        }

        private bool Validator(GovernanceValidationProfile validatedEntity, GovernanceValidationProfile referenceEntity)
        {
            var indicators = (List<GovernanceIndicator>)GetValue(validatedEntity);
            if (indicators == null || !indicators.Any())
            {
                return false;
            }

            return indicatorsHashSet.All(indicator => indicators.Where(x=> !x.Number.Contains("CG.6")).Count(x => string.Equals(x.Number, indicator, StringComparison.CurrentCultureIgnoreCase)) != 0);
        }
    }
}
